package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.ShopProgressBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

public class ShopProgressActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_progress);
        initData();
        initView();
    }

    private ImageView iv_logo;
    private TextView tv_state, tv_expressage, tv_numbers, tv_phone;
    private RecyclerView recycler;

    private void initView() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("订单跟踪");
        findViewById(R.id.btn_main_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        iv_logo = (ImageView) findViewById(R.id.iv_logo);
        tv_state = (TextView) findViewById(R.id.tv_state);
        tv_numbers = (TextView) findViewById(R.id.tv_numbers);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_expressage = (TextView) findViewById(R.id.tv_expressage);

        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    private String orderId;

    private void initData() {
        orderId = getIntent().getStringExtra("orderId");
        shopProgress(orderId);
    }

    ShopProgressBean.InfoEntity infoEnt = new ShopProgressBean.InfoEntity();

    private void shopProgress(String orderId) {
        if (orderId == null || orderId.equals("")) {
            MyToast("请刷新页面");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_EXPRESSINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("------", "R== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        //  MyToast(info);
                        //   upload(type);
                        Gson gson = new Gson();
                        ShopProgressBean bean = gson.fromJson(result, ShopProgressBean.class);
                        infoEnt = bean.getInfo();
                    } else {
                        if ("无效的订单".equals(info)) {

                            MyToast("此订单还没物流");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void setData() {
        /**
         *   private ImageView iv_logo;
         private TextView tv_state, tv_expressage, tv_numbers, tv_phone;
         private RecyclerView recycler;
         */
        x.image().bind(iv_logo, infoEnt.getPic_url());
        tv_state.setText(infoEnt.getOrder_status());
        tv_expressage.setText("承运公司：" + infoEnt.getExpress_name());
        tv_numbers.setText("运单编号：" + infoEnt.getExpress_no());
        tv_phone.setText(infoEnt.getExpress_tel().equals("") ? "暂无" : infoEnt.getExpress_tel());
        recycler.scrollToPosition(0);
        recycler.setAdapter(new BaseRVAdapter(this, infoEnt.getData()) {
            public int getLayoutId(int viewType) {
                return R.layout.progress_item;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                if (position != 0) {
                    holder.getImageView(R.id.progress_img).setBackgroundResource(R.drawable.progress_gray);
                    holder.getTextView(R.id.progress_content).setTextColor(getResources().getColor(R.color.text));
                    holder.getTextView(R.id.progress_time).setTextColor(getResources().getColor(R.color.text));
                } else {
                    holder.getImageView(R.id.progress_img).setBackgroundResource(R.drawable.progress_green);
                }
                holder.setTextView(R.id.progress_content, infoEnt.getData().get(position).getAcceptStation());
                holder.setTextView(R.id.progress_time, infoEnt.getData().get(position).getAcceptTime());

            }
        });
    }
}
